package com.ldk.framework.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ldk.base.framework.common.CustomException;
import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.framework.service.ILostOrderImagesService;
import com.ldk.framework.service.ILostOrderService;
import com.ldk.framework.dao.ILostOrderDAO;
import com.ldk.framework.entity.LostOrder;
import com.ldk.framework.entity.LostOrderImages;

/**
* LostOrderService接口实现类
* @author xiangwl
*/
@Service
public class LostOrderService extends BaseService<LostOrder> implements ILostOrderService {

	@Autowired
	private ILostOrderDAO lostOrderDAO;
	@Autowired
	private ILostOrderImagesService lostOrderImagesService;

	@Override
	protected IBaseDAO<LostOrder> getBaseDao() {
     	return this.lostOrderDAO;
	}
	
	@Override
	@Transactional(rollbackFor={Exception.class, CustomException.class, NullPointerException.class, SQLException.class, IOException.class})
	public Boolean publish(String dbKey, LostOrder model, List<LostOrderImages> images) throws Exception{
		if(model == null || images == null || images.isEmpty())
			return false;
		try{
			int row = insert(dbKey, model);
			if(row < 1)
				throw new CustomException("发布快递订单失败");
			for (LostOrderImages item : images) {
				row = lostOrderImagesService.insert("", item);
				if(row < 1){
					throw new CustomException("发布快递订单货物图片保存失败");
				}
			}
			return row > 0;
		}
		catch(Exception e){
			logger.error(e.getMessage());
			throw new CustomException(e.getMessage());
		}
	}

}
